package com.fz.service.project.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.project.projectPart.ProjectPartAdd;
import com.fz.dto.project.projectPart.ProjectPartQuery;
import com.fz.dto.project.projectPart.ProjectPartUpd;
import com.fz.entity.project.ProjectPart;
import com.fz.entity.system.DictionaryValue;
import com.fz.mapper.project.ProjectPartMapper;
import com.fz.service.project.ProjectPartService;
import com.fz.utils.JacksonUtils;
import com.fz.utils.StringUtils;
import com.fz.vo.project.projectPart.ProjectPartVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author :xmj
 * @date :2021/3/25 下午4:03
 */
@Service
@SuppressWarnings("unchecked")
public class ProjectPartServiceImpl implements ProjectPartService {

    @Resource
    private ProjectPartMapper projectPartMapper;

    /**
     * @param pageNum          pageNum
     * @param pageSize         pageSize
     * @param projectPartQuery projectPartQuery
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPart.ProjectPartVo>
     * @description 查询标段信息分页
     * @params
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    public ResponseEntity<ProjectPartVo> findProjectPartList(Integer pageNum, Integer pageSize, ProjectPartQuery projectPartQuery) {
        Page<ProjectPart> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProjectPart> lambdaQueryWrapper = new LambdaQueryWrapper<ProjectPart>() {
            {
                //TODO 查询条件条件添加 ->在此拼接
                eq(StringUtils.isNotEmpty(projectPartQuery.getStatus()), ProjectPart::getStatus, projectPartQuery.getStatus())
                        .eq(projectPartQuery.getId() != null, ProjectPart::getId, projectPartQuery.getId())
                        .like(projectPartQuery.getProjectId() != null, ProjectPart::getProjectId, projectPartQuery.getProjectId());
            }
        };
        IPage<ProjectPartVo> projectPartList = projectPartMapper.findProjectPartList(page, lambdaQueryWrapper);
        return ResponseEntity.buildSuccess(projectPartList);
    }

    /**
     * @param projectPartQuery projectPartQuery
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPart.ProjectPartVo>
     * @description 查询标段信息列表 不分页
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    public ResponseEntity<ProjectPartVo> selectProjectPartsList(ProjectPartQuery projectPartQuery) {
        LambdaQueryWrapper<DictionaryValue> queryWrapper = new LambdaQueryWrapper<>();
        //TODO 查询条件条件添加 ->在此拼接
        List<ProjectPartVo> projectPartVos = projectPartMapper.selectProjectPartList(queryWrapper);
        if (projectPartVos.isEmpty()) {
            return ResponseEntity.buildSuccess(null);
        } else {
            return ResponseEntity.buildSuccess(projectPartVos);
        }
    }

    /**
     * @param projectPartAdd projectPartAdd
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPart.ProjectPartVo>
     * @description 新增标段信息
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ProjectPartVo> saveProjectPart(ProjectPartAdd projectPartAdd) {
        ProjectPart projectPart = JacksonUtils.obj2pojo(projectPartAdd, ProjectPart.class);
        projectPartMapper.insert(projectPart);
        return ResponseEntity.buildSuccess(null);
    }

    /**
     * @param projectPartUpd projectPartUpd
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPart.ProjectPartVo>
     * @description 修改标段信息
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    public ResponseEntity<ProjectPartVo> updateProjectPart(ProjectPartUpd projectPartUpd) {
        ProjectPart projectPart = JacksonUtils.obj2pojo(projectPartUpd, ProjectPart.class);
        return ResponseEntity.buildSuccess(projectPartMapper.updateById(projectPart));
    }

    /**
     * @param ids  ids
     * @return com.fz.common.ResponseEntity<com.fz.vo.project.projectPart.ProjectPartVo>
     * @description 删除标段信息
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    public ResponseEntity<ProjectPartVo> delProjectPart(String ids) {
        Integer[] integers = Convert.toIntArray(ids);
        projectPartMapper.deleteBatchIds(Arrays.asList(integers));
        return ResponseEntity.buildSuccess();
    }
}
